import 'package:dio_wrapper/dio_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo2/dio_test/model/get_dog_breed_img_resp_data.dart';

class DioTestPage extends StatelessWidget {
  const DioTestPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("dio test"),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            MaterialButton(
              child: const Text("get with default base url and return json"),
              onPressed: () async {
                RespData res = await dio.network(
                  Method.get,
                  "breed/hound/images",
                  responseType: ResponseType.json,
                  params: {"params1": 1, "params2": "22"},
                  headers: {"header1": 1, "header2": "aa"},
                  logPrint: true,
                );
                if (null != res.data) {
                  final getDogBreedImgRespData = GetDogBreedImgRespData.fromJson(res.data);
                  debugPrint(getDogBreedImgRespData.toString());
                }
              },
            ),
            MaterialButton(
              child: const Text("get with default base url and return class"),
              onPressed: () async {
                RespData res = await dio.network(
                  Method.get,
                  "breed/hound/images",
                  responseType: ResponseType.json,
                  params: {"params1": 1, "params2": "22"},
                  headers: {"header1": 1, "header2": "aa"},
                  logPrint: true,
                  fromJson: (json) {
                    return GetDogBreedImgRespData.fromJson(json);
                  },
                );
                if (null != res.data) {
                  final getDogBreedImgRespData = res.data;
                  debugPrint(getDogBreedImgRespData.toString());
                }
              },
            ),
            MaterialButton(
              child: const Text("post with custom base url and return json"),
              onPressed: () async {
                RespData res = await dio.network(
                  Method.post,
                  "posts",
                  headers: {"header1": 1, "header2": "aa"},
                  baseUrl: "https://jsonplaceholder.typicode.com/",
                  responseType: ResponseType.json,
                  params: {"userId": 100, "title": "title", "body": "body"},
                  logPrint: true,
                );
                if (null != res.data) {
                  debugPrint(res.toString());
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}
